<?php

/**
 * Subclass for performing query and update operations on the 'reaction' table.
 *
 *
 *
 * @package lib.model
 */
class ReactionPeer extends BaseReactionPeer
{
	public static function saveReaction(User $user, $statement_id, $content=null, $embed_code=null, $embed_link=null){

		try {
				
			$reaction = new Reaction();
			$reaction->setUser($user);
			$reaction->setStatementId($statement_id);
			$reaction->setText($content);
			$reaction->setVideoRawEmbedCode($embed_code);
			$reaction->setVideoRawLink($embed_link);
			$reaction->save();
				
			$statement = LypoStatementPeer::retrieveByPK($statement_id);
			$user->setCachedStatementText($statement->getText());
			$user->setCachedReactionText($content);
			$user->setCachedStatementId($statement_id);
			$user->save();
		} catch (Exception $e){
			Throw $e;
		}
	}

	public static function getPerfectOppositesReactionsForStatement(User $user, $statement_id){
		$ret = $user->getPerfectOppositesIds(10);
		$c = new Criteria();
		$c->add(ReactionPeer::STATEMENT_ID,$statement_id);
		$c->add(ReactionPeer::USER_ID,$ret['ids'],Criteria::IN);
		$c->addDescendingOrderByColumn(ReactionPeer::CREATED_AT);
		return ReactionPeer::doSelectJoinUser($c);
	}

	public static function getReactionsForStatement($statement_id){
		$c = new Criteria();
		$c->add(ReactionPeer::STATEMENT_ID,$statement_id);
		$c->addDescendingOrderByColumn(ReactionPeer::CREATED_AT);
		return ReactionPeer::doSelectJoinUser($c);

	}

	public static function getNumRespectsForReaction(Reaction $reaction){
		//Tel aantal respects
		$c = new Criteria();
		$c->add(ReactionRespectPeer::REACTION_ID,$reaction->getId());
		return ReactionRespectPeer::doCount($c);
	}

	public static function giveRespectToReaction(User $user,$reaction_id){
		//Kijken of deze gebruiker al respect heeft gegeven aan deze reactie!
		$c = new Criteria();
		$c->add(ReactionRespectPeer::USER_ID,$user->getId());
		$c->add(ReactionRespectPeer::REACTION_ID,$reaction_id);
		$num = ReactionRespectPeer::doCount($c);
		if($num > 0) throw new sfException('User already respected this reaction');

		$newRespect = new ReactionRespect();
		$newRespect->setUser($user);
		$newRespect->setReactionId($reaction_id);
		$newRespect->save();

		//Gebruikers respects updaten (cachen eigenlijk...)
		self::updateAggregatedRespectsForReactionId($reaction_id);
	}

	public static function updateAggregatedRespectsForReactionId($reaction_id){
		//Update aantal respects voor de gebruiker in cached veld
		$reaction = ReactionPeer::retrieveByPK($reaction_id);
		if($reaction){
			//User respects updaten
			$c = new Criteria();
			$c->add(ReactionPeer::USER_ID,$reaction->getUserId());
			$c->addGroupByColumn(ReactionPeer::NUM_RESPECTS);
			$c->addAsColumn('total','SUM('.ReactionPeer::NUM_RESPECTS.')');
			/* @var $rs MysqlResultSet */
			$rs = ReactionPeer::doSelectRS($c);
			if($rs->next()){
				$num_respects = $rs->getInt(1+Reactionpeer::NUM_COLUMNS);
				$user = UserPeer::retrieveByPK($reaction->getUserId());
				$user->setNumRespects($num_respects);
				$user->save();
			}
				
			//Nu ook bij het statement updaten!
			$c = new Criteria();
			$c->add(ReactionPeer::STATEMENT_ID,$reaction->getStatementId());
			$c->addGroupByColumn(ReactionPeer::NUM_RESPECTS);
			$c->addAsColumn('total','SUM('.ReactionPeer::NUM_RESPECTS.')');
			/* @var $rs MysqlResultSet */
			$rs = ReactionPeer::doSelectRS($c);
			if($rs->next()){
				$num_respects = $rs->getInt(1+Reactionpeer::NUM_COLUMNS);
				$statement = LypoStatementPeer::retrieveByPK($reaction->getStatementId());
				$statement->setNumRespects($num_respects);
				$statement->save();
			}
				
				
		}


	}


	public static function getRespectedReactionIdsForUser(User $user){
		$ret = $user->getReactionRespects();
		if($ret){
			foreach($ret as $respected_reaction){
				$ret[$respected_reaction->getReactionId()]=1;
			}
			return $ret;
		}
		return array();
	}

}
